package com.example.layui_project.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class BestUitls {
    /**
     * 空验证
     * 
     * @param o 待验证的对象
     * @return
     */
    public static boolean isNotBlank(Object o) {

        return !(null == o || o.equals("") || o == "" || o.equals("null"));
    }

    public static Map<String, String> parseHttpGetParam(String param) {
        Map<String, String> retMap = new HashMap<String, String>();
        if (param == null) {
            return retMap;
        }

        String[] arrSplit = null;
        arrSplit = param.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");
            if (arrSplitEqual.length > 1) {
                retMap.put(arrSplitEqual[0], arrSplitEqual[1]);
            } else {
                if (arrSplitEqual[0] != "") {
                    retMap.put(arrSplitEqual[0], ""); // 只有参数没有值，不加入
                }
            }
        }
        return retMap;
    }

    public static String crateOrderId() {
        int ram4 = (int) ((Math.random() * 9 + 1) * 1000);
        String time = BestUitls.format(new Date(), "yyyyMMddHHmmssSSS");
        return time + ram4;
    }

    public static void main(String[] args) {
        System.out.println(BestUitls.crateOrderId());
    }

    /**
     * 模糊查询key
     * 
     * @param key
     * @param map
     * @param c
     * @return
     */
    public static <T> List<T> likeKey(String key, Map<String, T> map, Class<T> c) {
        Iterator<Entry<String, T>> it = map.entrySet().iterator();
        List<T> list = new ArrayList<T>();
        while (it.hasNext()) {
            Entry<String, T> en = it.next();
            String k = en.getKey().toLowerCase();
            // 目前写死最大返回值10
            // if (list.size() > 10) {
            // break;
            // }
            if (k.contains(key.toLowerCase())) {
                list.add(en.getValue());
            }
        }
        return list;
    }

    /**
     * 模糊查询key
     * 
     * @param key
     * @param map
     * @param c
     * @return
     */
    public static <T> List<T> likeKey(String key, Map<String, T> map, Class<T> c, int limit) {
        Iterator<Entry<String, T>> it = map.entrySet().iterator();
        List<T> list = new ArrayList<T>();
        while (it.hasNext()) {
            Entry<String, T> en = it.next();
            String k = en.getKey().toLowerCase();
            // 目前写死最大返回值10
            // if (list.size() >= (limit == 0 ? 1 : limit)) {
            // break;
            // }
            if (k.contains(key.toLowerCase())) {
                list.add(en.getValue());
            }
        }
        return list;
    }

    /**
     * list转换map
     * 
     * @param list 数组
     * @param keyMethodName 对象方法名
     * @param c 对象类型
     * @return
     */
    // 例
    // List<User> users = new ArrayList<>();
    // User u = new User();
    // u.setId(100L);
    // u.setName("呵呵");
    // users.add(u);
    // Map<Object, User> userMap = BestUitls.listToMap(users, "getId", User.class);
    public static <K, V> Map<K, V> listToMap(List<V> list, String keyMethodName, Class<V> c) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        if (list != null) {
            try {
                Method methodGetKey = c.getMethod(keyMethodName);
                for (int i = 0; i < list.size(); i++) {
                    V value = list.get(i);
                    @SuppressWarnings("unchecked")
                    K key = (K) methodGetKey.invoke(list.get(i));
                    if (key != null) {
                        map.put(key, value);
                    }
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("字段不能与键匹配!");
            }
        }

        return map;
    }

    public static boolean isNotBlank(Object[] os) {
        boolean isBlank = true;
        for (Object o : os) {
            isBlank = isNotBlank(o);
            if (!isBlank) {
                break;
            }
        }
        return isBlank;
    }

    private final static String DEFAULT_FORMAT_DATE = "yyyy-MM-dd HH:mm:ss";

    public static Date format(String d) {
        return format(d, DEFAULT_FORMAT_DATE);
    }

    public static Date format(String d, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            // 日期格式化
            return sdf.parse(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String format(Date data) {
        SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_FORMAT_DATE);
        return formatter.format(data);

    }

    public static String format(Date data, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        // 日期格式化
        return sdf.format(data);
    }


    /**
     * 8 * 对象转byte[] 9 * @param obj 10 * @return 11 * @throws IOException 12
     */

    public static byte[] object2Bytes(Object obj) throws IOException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);
        byte[] bytes = bo.toByteArray();
        bo.close();
        oo.close();
        return bytes;
    }

    /**
     * byte[]转对象
     * 
     * @param bytes
     * @return
     * @throws Exception
     */
    public static Object bytes2Object(byte[] bytes) throws Exception {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        ObjectInputStream sIn = new ObjectInputStream(in);
        return sIn.readObject();
    }

    /**
     * MAP转URL
     * 
     * @param map
     * @return
     */
    public static String mapToUrlParams(Map<String, Object> map) {
        if (map == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        return sb.toString();
    }

    public static String toBinary(String str) {
        char[] strChar = str.toCharArray();
        String result = "";
        for (int i = 0; i < strChar.length; i++) {
            result += Integer.toBinaryString(strChar[i]) + " ";
        }
        return result;
    }




}
